package com.example.untils;

import com.github.houbb.opencc4j.core.impl.ZhConvertBootstrap;
import com.github.houbb.opencc4j.support.segment.impl.Segments;
import com.github.houbb.opencc4j.util.ZhTwConverterUtil;

import java.lang.reflect.Field;

public class LanguageConversionUtils {

    public static <T> T convertToSimplifiedChinese(T input) {

        // 创建 OpenCC 转换器，选择繁体到简体
        Field[] fields = input.getClass().getDeclaredFields();
        for(Field field : fields) {
            field.setAccessible(true);
            Object o = null;
            try {
                o = field.get(input);
                if(o instanceof String) {
                    String str = (String) o;
                    if(ZhTwConverterUtil.containsTraditional(str)) {
                        str = ZhConvertBootstrap.newInstance().segment(Segments.chars())
                                .toSimple(str);
                    }
                    field.set(input, str);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return input; // 返回输入的泛型对象
    }

}
